package com.health.manage.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.manage.mapper.PharmacyMapper;
import com.health.manage.mapper.UserMapper;
import com.health.manage.pojo.dto.PharmacyDto;
import com.health.manage.pojo.entity.Pharmacy;
import com.health.manage.pojo.entity.User;
import com.health.manage.service.IPharmacyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 *
 * @Description: 用药记录业务处理
 * @author: pharmacy
 */
@Service
public class PharmacyServiceImpl extends ServiceImpl<PharmacyMapper, Pharmacy> implements IPharmacyService {

	@Autowired(required = false)
	private PharmacyMapper pharmacyMapper;
	@Autowired(required = false)
	private UserMapper userMapper;

	@Override
	public Boolean addData(Pharmacy pharmacy) {
		int result = this.pharmacyMapper.insert(pharmacy);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Boolean updateData(Pharmacy pharmacy) {
		int result = this.pharmacyMapper.updateById(pharmacy);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}


	@Override
	public Boolean deleteData(Long id) {
		int result = this.pharmacyMapper.deleteById(id);
		if(result > 1){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public Page<Pharmacy> getPageData(PharmacyDto pharmacyDto) {

		Page<Pharmacy> page = new Page<>(pharmacyDto.getCurrentPage(), pharmacyDto.getPageSize());
		QueryWrapper<Pharmacy> queryWrapper = new QueryWrapper<>();
		if(pharmacyDto.getUserId()!=null && !"".equals(pharmacyDto.getUserId())){
			queryWrapper.eq("user_id", pharmacyDto.getUserId());
		}
		if(pharmacyDto.getName()!=null && !"".equals(pharmacyDto.getName())){
			queryWrapper.like("name", pharmacyDto.getName());
		}
		if(pharmacyDto.getAccount()!=null && !"".equals(pharmacyDto.getAccount())){
			queryWrapper.apply("user_id in(select id from user where account like {0} )", "%" + pharmacyDto.getAccount() + "%");
		}
		Page<Pharmacy>records = this.pharmacyMapper.selectPage(page, queryWrapper);
		for(Pharmacy pharmacy : records.getRecords()){
			User user = this.userMapper.selectById(pharmacy.getUserId());
			if(user != null){
				pharmacy.setUser(user);
			}
			User creator = this.userMapper.selectById(pharmacy.getCreateId());
			if(creator != null){
				pharmacy.setCreator(creator);
			}
		}
		return records;
	}


}
